export function h5_urlToBase64(url) {
    return new Promise((resolve, reject) => {
        let image = new Image();
        image.onload = function () {
            let canvas = document.createElement("canvas");
            canvas.width = this.naturalWidth;
            canvas.height = this.naturalHeight;
            // 将图片插入画布并开始绘制
            canvas.getContext("2d").drawImage(image, 0, 0);
            // result
            let result = canvas.toDataURL("image/png");
            resolve(result);
        };
        // CORS 策略，会存在跨域问题https://stackoverflow.com/questions/20424279/canvas-todataurl-securityerror
        image.setAttribute("crossOrigin", "Anonymous");
        image.src = url;
        // 图片加载失败的错误处理
        image.onerror = () => {
            reject(new Error("urlToBase64 error"));
        };
    });
}

export function wx_imageToBase64(img) {
    return new Promise((resolve, reject) => {
        wx.downloadFile({
            url: img,
            success(res) {
                wx.getFileSystemManager().readFile({
                    filePath: res.tempFilePath, //选择图片返回的相对路径
                    encoding: "base64", //编码格式
                    success: (res) => {
                        //成功的回调
                        resolve("data:image/png;base64," + res.data);
                    },
                });
            },
        });
    });
}


export function App_imgToBase(url) {
    return new Promise((resolve) => {
        plus.io.resolveLocalFileSystemURL(url, function (entry) {
            entry.file(function (file) {
                var fileReader = new plus.io.FileReader();
                fileReader.readAsDataURL(file);
                fileReader.onloadend = function (evt) {
                    resolve(evt.target.result);
                };
            });
        });
    })
}